package com.scalar.chiptrack.operations.tapereel.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.shippingRequest.forms.ShippingRequestForm;
import com.scalar.chiptrack.operations.shippingRequest.ShippingRequest;
import com.scalar.chiptrack.operations.tapereel.forms.TapeReelRequestForm;
import com.scalar.chiptrack.operations.tapereel.TapeReelRequest;
import com.scalar.chiptrack.operations.tapereel.dao.TapeReelRequestDAO;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Apr 16, 2007
 * Time: 7:11:00 PM
 */
public class TapeReelRequestNewCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/tapeReelRequest.do";

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ScalarException
    {
        TapeReelRequestForm tapeReelRequestForm = (TapeReelRequestForm) form;
        TapeReelRequest tapeReelRequest         = tapeReelRequestForm.getTapeReelRequest();
        ActionForward actionForward             = mapping.findForward("newRequest");
        HttpSession mSession                    = request.getSession();
        User user                               = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );
        HashMap errors                          = new HashMap();
        HashMap info                            = new HashMap();

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);
        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME );
            return ( mapping.findForward ( "login" ) );
        }
        else
        {
            if ( user.isActionAllowed ( Functionality.TAPE_REEL_REQUEST , User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute ( "showMessage", "FALSE" );
            }
            else
            {
                request.setAttribute ( "showMessage", "TRUE" );
            }
        }
        try
        {

            Logger.debug( "In Tape and  Request new command" );
            tapeReelRequestForm.setTapeReelRequest( new TapeReelRequest() );

            request.setAttribute( "statusList", TapeReelRequest.getStatusList() );
            //request.setAttribute( "partNumbersList", NewProductDAO.loadPartNumbers( null, 0, Constants.LOCATION_TYPE_TAPE_AND_REEL, true, true, true, 0 ) );
            request.setAttribute( "partNumbersList", TapeReelRequestDAO.loadTapeAndReelFgProducts() );
            request.setAttribute( "vendorsList", TapeReelRequestDAO.getTrLocations() );

            //request.setAttribute("partNumbersList", TapeReelRequestDAO.getPartNumber( LOCATION_TYPE_CLASS_STORE ) );
        }
        catch ( Exception e )
        {
            // Writes this error information onto the screen
            Logger.error( "Error while processing the request for creating a new Tape and Reel request", e );
            errors.put( ERROR_INTERNAL_SERVER_ERROR, "Error while processing the request for creating a new Tape and Reel request" );

            tapeReelRequestForm.setTapeReelRequest( new TapeReelRequest() );
            request.setAttribute( "partNumberList", TapeReelRequestDAO.loadTapeAndReelFgProducts() );
            request.setAttribute( "vendorList", TapeReelRequestDAO.getTrLocations() );            
            request.setAttribute( "statusList", TapeReelRequest.getStatusList() );
            actionForward = mapping.findForward( "search" );
        }

        return  actionForward;
    }
}
